<template>
    <div class="goodsDetailContainer">
        <div class="card" @click="previewImgs">
            <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
                <van-swipe-item v-for="item in imgs" :key="item.src">
                    <img :src="item.src" alt="">
                </van-swipe-item>
            </van-swipe>
        </div>
        <div class="card">
            <div class="price">
                <span class="sell_price">&yen; {{ goodsInfo.sell_price }}</span>
                <span class="market_price">&yen; {{ goodsInfo.market_price }}</span>
            </div>
            <div class="title van-multi-ellipsis--l2">
                {{ goodsInfo.title }}
            </div>
        </div>
        <div class="card">
            <van-divider content-position="left">介绍</van-divider>
            <div class="content" v-html="goodsInfo.content"></div> 
        </div>
        <van-goods-action>
            <van-goods-action-icon icon="chat-o" text="客服" />
            <van-goods-action-icon icon="cart-o" text="购物车" badge={{item}} />
            <van-goods-action-button type="warning" text="加入购物车1"  @click="addCar(true)"/>
            <van-goods-action-button type="danger" text="立即购买1" @click="buy(false)"/>
        </van-goods-action>

        <!-- sku -->
        <!-- 商品sku -->
        <van-sku 
            v-model="showSku" 
            :show-add-cart-btn="isShowAddCartBtn"
            :sku="sku" 
            :goods="goods" 
            :goods-id="goodsInfo.id" 
            :hide-stock="sku.hide_stock"
            :reset-stepper-on-hide="true" 
            @add-cart="addCart"
            @buy-clicked="buyClicked"
           >
            <template #sku-messages>
               <div class="card">
                    <van-divider>文字</van-divider>
                    <div>商品货号：{{ goodsInfo.goods_no }}</div>
                    <div>库存：{{ goodsInfo.stock_quantity }}件</div>
                    <div>上架时间：{{ goodsInfo.add_time | dateFormat("YYYY-MM-DD") }}</div>
                </div>
            </template>
        </van-sku>
    </div>
</template>

<script>
import {fetchGoodsImg,fetchGoodsInfo} from "../api/goodsdetail.js"
import { ImagePreview} from 'vant';
// ImagePreview(['https://img01.yzcdn.cn/vant/apple-1.jpg']);
export default {
    data() {
        return {
            id: this.$route.params.id,
            isShowAddCartBtn:false,
            imgs: [],
            goodsInfo:{},
            showSku: false,
            sku: {
                // 数据结构见下方文档
                tree: [],
                price: "0.00", // 默认价格（单位元）
                stock_num: 0, // 商品总库存
                hide_stock: false, // 是否隐藏剩余库存
            },
            goods: {
                // 数据结构见下方文档
                picture: ""
            }
        }
    },
    created() {
        this._fetchGoodsImg()
        this._fetchGoodsInfo()
    },
    methods:{
        // 获取商品的图片集合
        async _fetchGoodsImg(){
           const {message} =  await fetchGoodsImg(this.id)
           this.imgs = message;
            // 给sku缩略图赋值
            this.goods.picture = this.imgs[0].src
        },
        // 获取商品信息
        async _fetchGoodsInfo(){
            let {message} = await fetchGoodsInfo(this.id)
            // 把富文本中的图片宽度替换成空，才不会撑开页面
            let reg = /\width=['"]\w+['"]\s/ig;
            message.content =  message.content.replace(reg,'')
            // console.log(message.content)
            this.goodsInfo = message;
            // 给sku赋值
            console.log(this.goodsInfo)
            this.sku.price = this.goodsInfo.sell_price;
            this.sku.stock_num = this.goodsInfo.stock_quantity;
           
        },
        // 预览图片
        previewImgs(){
            // 去除图片路径，构造一个数组
            let imgs = this.imgs.map( item => item.src )
            ImagePreview(imgs);
        },
        // 加入购物车
        addCar(bool){
            this.showSku = true;
            this.isShowAddCartBtn = bool
        },
        // 立即购买
        buy(bool){
            this.showSku = true;
            this.isShowAddCartBtn = bool
        },
        // sku 加入商品到购物，依然停留在当前页面
        addCart(skuData){
            // 将商品添加到购物车（vuex）
            // 构造购物车商品数据结构
            let {goodsId, selectedNum} = skuData;
            let price = this.goodsInfo.sell_price;
            let item = {id:goodsId,number:selectedNum,price:price,selected:true}
            // 提交一个mutation
            this.$store.commit('addGoodsToCar',item)
            this.showSku = false;
            this.$dialog.alert({
                message:'加入购物车成功'
            })
            
        },
        // sku 立即购买，跳转到购物车页面
        buyClicked(skuData){
            // 将商品添加到购物车（vuex）
            // 构造购物车商品数据结构
            let {goodsId, selectedNum} = skuData;
            let price = this.goodsInfo.sell_price;
            let item = {id:goodsId,number:selectedNum,price:price,selected:true}
            // 提交一个mutation
            this.$store.commit('addGoodsToCar',item)
            this.showSku = false;
            this.$router.push('/home/shopcar')
        }
    }
}
</script>

<style lang="scss" scoped>
.goodsDetailContainer {
    height: 100vh;
    background: rgba(234, 231, 231, 0.368627451);
    padding: 4px;

    .card {
        border-radius: 6px;
        margin-bottom: 8px;
        background: #fff;
        padding: 8px;

        .my-swipe {
            .van-swipe-item {
                height: 240px;
                text-align: center;

                img {
                    // width: 100%;
                    height: 100%;
                }
            }
        }

        .price {
            .sell_price {
                color: #fa865d;
                font-size: 20px;
                margin-right: 20px;
            }

            .market_price {
                color: #666;
                font-size: 14px;
                text-decoration: line-through;
            }
        }

        .title {
            color: #333;
            font-weight: 500;
        }

        .content {
            // 深度选择器（可以对组件进行样式穿透）
            ::v-deep img {
                width: 100%;
                height: 100%;
            }
        }
    }
}
</style>